package com.zf.shuashua.advert;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.meis.base.mei.base.BaseActivity;
import com.meis.base.mei.utils.Eyes;
import com.zf.shuashua.R;

import java.util.HashMap;
import java.util.Map;

/**
 * 广告详情页
 */
public class AdvertDetailActivity extends BaseActivity {

    public static final String KEY_URL = "key_url";
    private WebView webView;
    private String url;

    public static Intent createIntent(Context context, String url) {
        final Intent intent = new Intent(context, AdvertDetailActivity.class);
        intent.putExtra(KEY_URL, url);
        return intent;
    }

    @Override
    protected void initView() {
        Eyes.translucentStatusBar(this,true);
        webView = findViewById(R.id.webView);
        url = getIntent().getStringExtra(KEY_URL);
        initWebView();
        loadWeb();
    }

    private void initWebView() {
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                if (!view.getSettings().getLoadsImagesAutomatically()) {
                    view.getSettings().setLoadsImagesAutomatically(true);
                }
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();  // 接受所有网站的证书
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
//                if (TextUtils.isEmpty(mTitle)) {
//                    binding.setTitle(title);
//                }
            }
        });

        WebSettings ws = webView.getSettings();

        ws.setJavaScriptEnabled(true);//
        //开启 DOM storage API 功能 较大存储空间，使用简单
        ws.setDomStorageEnabled(true);
        ws.setDatabaseEnabled(true);
        ws.setGeolocationEnabled(true);
        ws.setBlockNetworkImage(false);//解决图片加载不出来的问题
        String dir = getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        ws.setCacheMode(WebSettings.LOAD_DEFAULT);//根据cache-control决定是否从网络上取数据。
        ws.setGeolocationDatabasePath(dir);
        ws.setGeolocationEnabled(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        //图片加载
        if(Build.VERSION.SDK_INT >= 19){
            ws.setLoadsImagesAutomatically(true);
        }else {
            ws.setLoadsImagesAutomatically(false);
        }

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int mDensity = metrics.densityDpi;

        if (mDensity == 120) {
            ws.setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
        }else if (mDensity == 160) {
            ws.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        }else if (mDensity == 240) {
            ws.setDefaultZoom(WebSettings.ZoomDensity.FAR);
        }
    }

    protected void loadWeb() {
        if (webView != null && !TextUtils.isEmpty(url)) {
            synCookies(this, url);
            webView.loadUrl(url);
        }
    }

    protected void synCookies(Context context, String url) {
//        CookieSyncManager.createInstance(context);
//        CookieManager cookieManager = CookieManager.getInstance();
//        cookieManager.setAcceptCookie(true);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_advert_detail;
    }

}
